//author:jxx
//此处是对表单的方法，组件，权限操作按钮等进行任意扩展(方法扩展可参照SellOrder.js)
let list =[];
let short
let extension = {
  components: {//动态扩充组件或组件路径
    //表单header、content、footer对应位置扩充的组件
    gridHeader: '',//{ template: "<div>扩展组xx件</div>" },
    gridBody: '',
    gridFooter: '',
    //弹出框(修改、编辑、查看)header、content、footer对应位置扩充的组件
    modelHeader: '',
    modelBody: '',
    modelFooter: ''
  },
  buttons: { view: [], box: [], detail: [] },//扩展的按钮
  methods: {//事件扩展
    getOption (field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        })
      })
      return option;
    },
    onInit () {
      
      this.single = true;
      this.textInline = true;
      this.ck = false;
      //;
      let templateType = this.getOption("templateType");
      let spare15 = this.getOption("spare15");
      let templateContent = this.getOption("templateContent");
      templateType.onChange = (val, option) => {
        this.editFormFields[spare15.field] = -1;
        //通过http从后台加载数据源
        this.http.post("/api/Dictionary/GetVueDictionary", ['MessageTemplate','shortmessage'], true).then(source => {
          //重新绑定数据源,select2.data.splice(0)会刷新所有相关的数据源(不建议使用)
          list = source[0].data.filter(item => item.parentID == val);
          short=source[1].data
          spare15.data = list;
        })
    
     

      }
      
      spare15.onChange = (val, option) => {
        let item = option.data.filter(item => item.key == val);
        console.log(item);
        this.editFormFields[templateContent.field] = item[0].value;
        //通过http从后台加载数据源
      }
      templateContent.onKeyPress =  (val, option) => {
        if(val.keyCode==125){
          console.log(this.editFormFields[templateContent.field] )
          this.updataData()
        }
        };
    },
    updataData(){
      let length =this.editFormOptions.length
      let templateContent = this.getOption("templateContent");
      var res = this.editFormFields[templateContent.field].match(/\$\{.*?\}/g) ;
      if(res){
        res = res.sort()
        var arrry= [res[0]];
        for (var i = 1; i < res.length; i++) {
            if (res[i] !== res[i-1]) {
                arrry.push(res[i]);
            }
        }
        console.log(arrry)
       list=[]
       console.log()
       arrry.forEach((item)=>{
       list.push(item.slice(2,item.length-1))
       })
       if(length>8 && list.length){
        this.editFormOptions.splice(4, list.length);
       }
       console.log(list)
       this.http.post("/api/Dictionary/GetVueDictionary", ['MessageTemplate','shortmessage'], true).then(source => {
        //重新绑定数据源,select2.data.splice(0)会刷新所有相关的数据源(不建议使用)
       let short2=source[1].data
        list.forEach((item)=>{
          // shortmessage
          let list5 = [{"dataKey":"shortmessage","data":short2,"title":item,"field":item,"colSize":12,"type":"select"}]
          this.editFormOptions.splice(4, 0, list5);
        })
      })
      
      }
    },
    rest(){
      this.editFormOptions=[[{"dataKey":"MessageType","data":[],"title":"短信类型","field":"templateType","colSize":12,"type":"select","required":true,}],
      [{"dataKey":"MessageTemplate","data":[],"title":"常用模板","field":"spare15","colSize":12,"type":"select"}],
      [{"title":"模板名称","required":true,"field":"templateName","colSize":12,"type":"text"}],
      [{"title":"模板内容","required":true,"field":"templateContent","colSize":12,"type":"textarea"}],
      [{"title":"备注","field":"remark","required":true,"colSize":12,"type":"textarea"}],
      [{"title":"失败原因","field":"auditreason","required":true,"colSize":12,"type":"textarea","disabled":true,"hidden":true,}],
      [{"title":"创建人","field":"creator","disabled":true,"type":"text"},
       {"title":"创建时间","field":"createDate","disabled":true,"type":"datetime"}],
      [{"title":"修改人","field":"modifier","disabled":true,"type":"text"},
       {"title":"修改时间","field":"modifyDate","disabled":true,"type":"datetime"}]]
    },
    addBefore(formData) { //新建保存前formData为对象，包括明细表
      return this.veriFromTable(formData)
    },
    updateBefore(formData) { //编辑保存前formData为对象，包括明细表、删除行的Id
      return this.veriFromTable(formData)
    },
    // 保存前编辑 
    veriFromTable(formData) {
      if(this.editFormOptions.length>8){
        this.updataData()
      }
     let obj={}
     let optionList=[]
      if(list.length!=0){
        list.forEach((item)=>{
          let listObj={}
          listObj={
            key:item,
            value:formData.mainData[item]
          }     
          let value=formData.mainData[item]
          obj[item]=value
          optionList.push(listObj)
        })
      }
      // let length =this.editFormOptions.length
      // if(length>8){
      //   this.editFormOptions.splice(4, list.length,);
      //  }
       formData.mainData.spare10=JSON.stringify(obj)
       formData.mainData.spare11=optionList
      return true
    },
    rowClick ({ row, column, event }) { //查询界面table点击行选中当前行
      this.$refs.table.$refs.table.toggleRowSelection(row);
      return true;
    },
    
    arrayToTree(arr) {
      const cloneData = JSON.parse(JSON.stringify(arr)) // 深拷贝避免影响源数据
      return cloneData.filter(father => {
        // 循环所有项，并添加children属性
        const branchArr = cloneData.filter(child => father.key === child.parentID) // 筛选出此项的所有子元素
        branchArr.length > 0 ? (father.children = branchArr) : false // 给父级添加一个children属性，并赋值
        // father.children = branchArr // 如果希望每个元素无论有没有children都添加children属性  可以直接赋值
        return father.parentID === 0 // 返回pid为''的顶级节点
      })
    },
    modelOpenBefore(row){
      console.log(row)
      console.log(this.editFormOptions.length,list,"222222222222")
      if(this.editFormOptions.length>8 && row.spare11){
      let list33 = JSON.parse(row.spare11);
        this.editFormOptions.splice(4, list33.length);
      }else if (this.editFormOptions.length>8 && list){
        // console.log(list,"list")
        // this.editFormOptions.splice(4, list.length);
        this.rest()
      }
    },
    modelOpenAfter(row) {
      this.http.post("/api/Dictionary/GetVueDictionary", ['MessageTemplate','shortmessage','cardmessage'], true).then(source => {
        //重新绑定数据源,select2.data.splice(0)会刷新所有相关的数据源(不建议使用)
      let short1=source[1].data
      let short2=source[2].data
      if(short1.length>0){
        short1.forEach((item)=>{
          item.parentID=111
        })
      }
         if(short2.length>0){
        short2.forEach((item)=>{
          item.parentID=222
          short1.push(item)
        })
        let obj1={key: 222, parentID:0,value:"卡短信参数"}
        let obj2={key: 111, parentID:0,value:"订单短信参数"}
        short1.push(obj1)
        short1.push(obj2)
        if(short1.length>0){
          short1.forEach((node,index)=>{
            node.label = node.value;
            node.value = node.key;
          })
        }
     
      }
   let dataList=this.arrayToTree(short1)
      console.log(dataList)
      //获取当前弹出框是新建还是编辑状态
      let isEDIT = this.currentAction == this.const.EDIT;
      if(isEDIT){
    if(row.spare11){
      let list = JSON.parse(row.spare11);
      if(list){
        for(var i=0;i<list.length;i++){
          let list5 = [{"dataKey":"shortmessage","data":dataList,"title":list[i].key,"field":list[i].key,"colSize":12,cascaderValue: true,"type":"cascader"}]
          console.log(list5)
          this.editFormOptions.splice(4, 0, list5);
          // this.editFormOptions.push(list5);
          this.editFormFields[list[i].key]=list[i].value
        }
      }
    }
      }
    })
    },

    async modelOpenBeforeAsync (row) {
      let auditreason = this.getOption("auditreason");
      if (row.auditStatus != 1) {
        this.editFormFields[auditreason.field] = row.auditreason;
        if (row.auditreason) { auditreason.hidden = false; }
        return true;
      } else {
        this.$error("审核通过的模板不能修改!");
        return false;
      }
    }
  }
};
export default extension;